package com.idconfirmsystem.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.idconfirmsystem.R;
import com.idconfirmsystem.model.Notice;
import com.idconfirmsystem.util.StringUtils;

public class NoticeAdapter extends BaseAdapter {

	private List<Notice> noticeList=new ArrayList<Notice>();
	private Context context;
	ListView listView;

	public NoticeAdapter(Context context, ListView listView) {
		this.context = context;
		this.listView = listView;
	}

	public NoticeAdapter(Context context, ListView listView, List<Notice> noticeList) {
		this.context = context;
		this.listView = listView;
		this.noticeList = noticeList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return noticeList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return noticeList != null ? noticeList.get(arg0) : null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
	public void resetData(List<Notice> newNotices){
		this.noticeList.clear();
		this.noticeList.addAll(newNotices);
		this.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.notice_item, null);
			viewHolder = new ViewHolder();
			viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
			viewHolder.tvReadCount = (TextView) convertView.findViewById(R.id.tv_read_count);
			viewHolder.tvSource = (TextView) convertView.findViewById(R.id.tv_source);
			viewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.ivTodayIcon = (ImageView) convertView.findViewById(R.id.iv_tip);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Notice item = noticeList.get(position);
		viewHolder.tvTitle.setText(item.getTitle());
		viewHolder.tvContent.setText(item.getContent());
		viewHolder.tvReadCount.setText(item.getRead_status());// ?
		// viewHolder.tvTime.setText("通知时间："+StringUtils.friendly_time(item.getNotice_date()));
		if (!StringUtils.isEmpty(item.getCreate_time())) {
			viewHolder.tvTime.setText("通知时间:" + StringUtils.friendly_time(item.getCreate_time()));
		}
		viewHolder.tvSource.setText("发送人：" + item.getSource_number());
		if (item.getNotice_date() != null && StringUtils.isToday(item.getNotice_date().toString())) {
			viewHolder.ivTodayIcon.setVisibility(View.VISIBLE);
		} else {
			viewHolder.ivTodayIcon.setVisibility(View.GONE);
		}
		return convertView;
	}

	public class ViewHolder {
		public TextView tvTitle;
		public TextView tvContent;
		public TextView tvTime;
		public TextView tvReadCount;
		public TextView tvSource;
		public ImageView ivTodayIcon;
	}
}
